package inet.app.view.dialog;

import inet.app.R;
import inet.app.model.util.StringUtil;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

public class ForgetPassDialog extends DialogFragment {

	private TextView mErrorText;
	private EditText mEmailEdt;

	private Context mContext;
	private String mEmail;

	public ForgetPassDialog(Context context) {
		this.mContext = context;
	}

	public static ForgetPassDialog newInstance(Context context) {
		ForgetPassDialog forgetPassDialog = new ForgetPassDialog(context);
		return forgetPassDialog;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = new Dialog(mContext, android.R.style.Theme_Dialog);
		dialog.setCanceledOnTouchOutside(false);
		dialog.setCancelable(false);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		View view = LayoutInflater.from(mContext).inflate(
				R.layout.view_forget_password, null);

		mEmailEdt = (EditText) view.findViewById(R.id.email_edt);
		mErrorText = (TextView) view.findViewById(R.id.error_tv);

		view.findViewById(R.id.auth_button).setOnClickListener(mAuthListener);
		dialog.setContentView(view);

		return dialog;
	}

	View.OnClickListener mAuthListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			auth();
		}
	};

	private void auth() {
		mEmail = mEmailEdt.getText().toString();
		boolean isValid = validate();
		if(isValid) {
			implementAuth();
		}
	}

	private boolean validate() {
		String mError = "";
		if(TextUtils.isEmpty(mEmail)) {
			mError = "Điền email";
		} else if(!StringUtil.validateEmail(mEmail)) {
			mError = "Email không hợp lệ";
		}
		if (!TextUtils.isEmpty(mError)) {
			mErrorText.setVisibility(View.VISIBLE);
			mErrorText.setText(mError);
			return false;
		} else {
			mErrorText.setVisibility(View.GONE);
			return true;
		}
		
	}

	private void implementAuth() {

	}

}
